
CREATE PROCEDURE usp_CreateJudgementDocument
	@ManuID int,
	@Title varchar(1000),
	@DecidedOn DateTime,
	@AppealNo VARCHAR(100),
    @CaseNotes VARCHAR(300),
    @EqCitation VARCHAR(100),
    @Judges VARCHAR(200),
    @JudgesCount INT,
    @JuridictionID INT,
    @CourtID INT,
    @LawyersNames VARCHAR(300),
    @RespondantLawyers VARCHAR(300),
    @CommonLawyers VARCHAR(300),
    @IssuingYear INT,
    @TypeOfLawID INT,
	@LinkProcessingRequired INT
AS
BEGIN
	DECLARE @TaskID INT
	DECLARE @DocumentID INT

	DECLARE @ImportRemarks VARCHAR(500)
	DECLARE @CurrentDate DATETIME
	DECLARE @CreatedUpdatedBy INT

	SET @ImportRemarks = 'Bulk Import'
	SET @CurrentDate = GetDate()	
	SET @CreatedUpdatedBy = 1

	BEGIN TRY
		BEGIN TRANSACTION

		INSERT INTO [dbo].[Task]
				   ([Notes]
				   ,[AssignedTo]
				   ,[TaskType]
				   ,[CompletionDate]
				   ,[CreatedBy]
				   ,[CreatedDate]
				   ,[ModifiedBy]
				   ,[ModifiedDate])
			 VALUES
				   (@ImportRemarks
				   ,0
				   ,1
				   ,@CurrentDate
				   ,@CreatedUpdatedBy
				   ,@CurrentDate
				   ,@CreatedUpdatedBy
				   ,@CurrentDate)

		SET @TaskID = SCOPE_IDENTITY() 

		-- Task with status Created
		INSERT INTO [dbo].[TaskStatus]
			   ([TaskId]
			   ,[Status]
			   ,[Remarks]
			   ,[CreatedBy]
			   ,[CreatedDate])
		 VALUES
			   (@TaskID
			   ,0
			   ,@ImportRemarks
			   ,@CreatedUpdatedBy
			   ,@CurrentDate)

			   -- Task with status For Review
		INSERT INTO [dbo].[TaskStatus]
			   ([TaskId]
			   ,[Status]
			   ,[Remarks]
			   ,[CreatedBy]
			   ,[CreatedDate])
		 VALUES
			   (@TaskID
			   ,1
			   ,@ImportRemarks
			   ,@CreatedUpdatedBy
			   ,@CurrentDate)

			   -- Task with status Approved
		INSERT INTO [dbo].[TaskStatus]
			   ([TaskId]
			   ,[Status]
			   ,[Remarks]
			   ,[CreatedBy]
			   ,[CreatedDate])
		 VALUES
			   (@TaskID
			   ,2
			   ,@ImportRemarks
			   ,@CreatedUpdatedBy
			   ,@CurrentDate)

			   -- Task with status Published
		INSERT INTO [dbo].[TaskStatus]
			   ([TaskId]
			   ,[Status]
			   ,[Remarks]
			   ,[CreatedBy]
			   ,[CreatedDate])
		 VALUES
			   (@TaskID
			   ,5
			   ,@ImportRemarks
			   ,@CreatedUpdatedBy
			   ,@CurrentDate)

		INSERT INTO [dbo].[DocumentMaster]
			   ([Name]
			   ,[Notes]
			   ,[Description] 
			   ,[IsPublished]
			   ,[TaskId]
			   ,[CreatedBy]
			   ,[CreatedDate]
			   ,[ModifiedBy]
			   ,[ModifiedDate])
		 VALUES
			   (@Title
			   ,@ImportRemarks
			   ,(CASE @LinkProcessingRequired WHEN 1 THEN 'LINKS' ELSE '' END)
			   ,@LinkProcessingRequired
			   ,@TaskID
			   ,@CreatedUpdatedBy
			   ,@CurrentDate
			   ,@CreatedUpdatedBy
			   ,@CurrentDate)

		SET @DocumentID = SCOPE_IDENTITY()

		UPDATE DocumentMaster set 
		Path = 'PublishedDocs/' + CAST(MONTH(@CurrentDate) as Varchar(2))
		+ CAST(YEAR(@CurrentDate) as Varchar(4)) + '/SectionDocs/' + CAST(@DocumentID as Varchar(10))
		+ '/Doc_' + CAST(@DocumentID as Varchar(10)) + '.pdf' 
		WHERE DocumentID = @DocumentID

		-- Insert Equivalant Citations into Other Citations
		INSERT INTO DocumentOtherCitations(DocumentId,CitationCompany,CitationNo) VALUES(@DocumentID,'Others',@EqCitation)
		
		INSERT INTO [dbo].[DocumentMeta]
			   ([DocumentID]
			   ,[DocumentType]
			   ,[DocumentTitle]
			   ,[DocumentDesc]
			   ,[DocumentIssueDate]
			   ,[DocumentNumber]
			   ,[DocumentStatus]
			   ,[DocumentEnfDate]
			   ,[CaseNote]
			   ,[EquivalentCitation]
			   ,[InfoOneCitation]
			   ,[JudgesNames]
			   ,[Bench]
			   ,[Juridiction]
			   ,[CourtNameId]
			   ,[LawyersNames]
			   ,[RespondantLawyers]
			   ,[CommonLawyers]
			   ,[PetitionNo]
			   ,[StatusChangeDate]
			   ,[IssueYear]
			   ,[TypeOfLawID]
			   ,[JudgementDate]
			   ,[IsPrimary]
			   ,[PriorHistory]
			   ,[ManupatraCitation]
			   ,[CasesRefferedto]
			   ,[ActsReffered])
		 VALUES
			   (@DocumentID
			   ,1
			   ,@Title
			   ,@Title
			   ,@DecidedOn
			   ,''
			   ,0
			   ,@DecidedOn
			   ,@CaseNotes
			   ,@EqCitation
			   ,NULL
			   ,@Judges
			   ,@JudgesCount
			   ,@JuridictionID
			   ,@CourtID
			   ,@LawyersNames
			   ,@RespondantLawyers
			   ,@CommonLawyers
			   ,@AppealNo
			   ,@CurrentDate
			   ,@IssuingYear
			   ,@TypeOfLawID
			   ,@DecidedOn
			   ,1
			   ,@PriorHistory
			   ,@ManuID
			   ,@CasesReffered
			   ,@ActsReffered)

		INSERT INTO [dbo].[DocumentSections]
			   ([DocumentID]
			   ,[SectionName]
			   ,[SectionCopyName]
			   ,[Sequence]
			   ,[Path]
			   ,[PublishedPath])
		 VALUES
			   (@DocumentID
			   ,@Title
			   ,'abc.docx'
			   ,1
			   ,NULL
			   ,'published path')

	INSERT INTO [dbo].[DocumentAppellantRespondent]
           ([DocumentID]
           ,[IsPrimary]
           ,[Appellant]
           ,[Respondent])
     VALUES
           (@DocumentID
           ,@IsPrimaryAppelant
           ,@Appellant
           ,@Respondent)

		COMMIT TRANSACTION
	END TRY
	BEGIN CATCH
		ROLLBACK TRANSACTION
	END CATCH

END